package org.clockworkmages.games.anno1186.situations.camp.options;

import org.clockworkmages.games.anno1186.GameBeansContext;
import org.clockworkmages.games.anno1186.GameGuiService;
import org.clockworkmages.games.anno1186.GameStateService;
import org.clockworkmages.games.anno1186.dao.FileUtil;
import org.clockworkmages.games.anno1186.dao.SerializationUtil;
import org.clockworkmages.games.anno1186.model.option.Option;

public class SelectSaveSlotOption
		extends Option {

	private GameStateService gameStateService;
	private GameGuiService gameGuiService;
	private String fileName;

	public SelectSaveSlotOption(int index, String fileName, boolean overwrite) {
		gameStateService = GameBeansContext.getBean(GameStateService.class);
		gameGuiService = GameBeansContext.getBean(GameGuiService.class);

		setLabel("Save slot " + index + " ("
				+ (overwrite ? "overwrite" : "empty") + "");
		this.fileName = fileName;
	}

	@Override
	public void select() {
		String gameStateSerialized = SerializationUtil.serialize(
				gameStateService.getGameState(), true);
		FileUtil.write("./" + this.fileName, gameStateSerialized);
		gameGuiService.addInfo("Games saved succesfully as '" + this.fileName
				+ "'");
		this.setGoBack(true);
		super.select();
	}
}
